﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSI.Models;
using System.Data;
using System.Text;
using System.Collections;
using PSI.Helper;

namespace PSI.Services {
    public class BaseCodeCategoryService : PSIBaseService {

        public Result AddOrEditCategory(BaseCodeCategory model) {
            var result = 0;

            if (string.IsNullOrEmpty(model.CategoryId)) {
                var str = new StringBuilder("insert psi_basecode_category values (@category_id,@category_name)");
                var p = CreateDbParameters();

                p.AddWithValue("category_id", Guid.NewGuid().ToString());
                p.AddWithValue("category_name", model.CategoryName);

                result = Execute(str.ToString(), p);
            } else {

                var str = new StringBuilder("update psi_basecode_category set category_name=@category_name where category_id=@category_id");

                var p = CreateDbParameters();

                p.AddWithValue("category_name", model.CategoryName);
                p.AddWithValue("category_id", model.CategoryId);

                result = Execute(str.ToString(), p);
            }

            if (result > 0)
                return new Result {Success=true };
            else
                return new Result { Success = true, Messgae = "服务器延时或网络问题,添加失败！" };

        }

        public Result DelCategory(BaseCodeCategory model) {
            var str = new StringBuilder("delete psi_basecode_category where category_id=@category_id");

            var p = CreateDbParameters();
            p.AddWithValue("category_id", model.CategoryId);

            var result = Execute(str.ToString(), p);


            if (result > 0)
                return new Result { Success = true};
            else
                return new Result { Success = false, Messgae = "服务器延时或网络问题,添加失败！" };

        }

        public List<BaseCodeMetadata> GetMetadataList(string CategoryId) {
            var result = new List<BaseCodeMetadata>();

            if (!string.IsNullOrEmpty(CategoryId)) {


                var str = "select basecode_id,basecode_memo,basecode_name,table_name from psi_basecode_metadata where category_id=@cateid";

                var p = CreateDbParameters();

                p.AddWithValue("cateid", CategoryId);

                Query(str, p, ready => {
                    result.Add(new BaseCodeMetadata {
                        BasecodeId = ready.GetString(ready.GetOrdinal("basecode_id")),
                        CategoryId = CategoryId,
                        BasecodeMemo = ready.GetString(ready.GetOrdinal("basecode_memo")),
                        BasecodeName = ready.GetString(ready.GetOrdinal("basecode_name")),
                        TableName = ready.GetString(ready.GetOrdinal("table_name")),

                    });
                });

            }

            return result;
        }

        public List<BaseCodeCategory> GetCategoryItem() {
            var str = new StringBuilder("select category_id,category_name from  psi_basecode_category");

            var result = new List<BaseCodeCategory>();

            Query(str.ToString(), ready => {
                result.Add(new BaseCodeCategory {
                    CategoryId = ready.GetString(ready.GetOrdinal("category_id")),
                    CategoryName = ready.GetString(ready.GetOrdinal("category_name")),
                    GetAllMetadata = GetMetadataList(ready.GetString(ready.GetOrdinal("category_id")))

                });

            });




            return result;
        }

        public List<TreeClass<BaseCodeMetadata>> GetTreeItem() {


            var item = new BaseCodeCategoryService().GetCategoryItem();

            var json = new List<TreeClass<BaseCodeMetadata>>();

            for (int i = 0; i < item.Count; i++) {
               

                if (item[i].GetAllMetadata.Count == 0) {
                    var objects = new TreeClass<BaseCodeMetadata>() {
                        Name = item[i].CategoryName, Id = item[i].CategoryId, Expandable = false 
                    };
                    json.Add(objects);
                } else {
                    var childs = new List<BaseCodeMetadata>();
                    for (int j = 0; j < item[i].GetAllMetadata.Count; j++) {
                        var child = new BaseCodeMetadata();
                        child.Name = item[i].GetAllMetadata[j].BasecodeName;
                        child.Leaf = true;
                        child.TableName = item[i].GetAllMetadata[j].TableName;
                        child.Id = item[i].GetAllMetadata[j].BasecodeId;
                        childs.Add(child);
                    }

                    var objects = new TreeClass<BaseCodeMetadata>(){ Name = item[i].CategoryName, Id = item[i].CategoryId, Children = childs, Expandable = true, Expanded = true };
                    json.Add(objects);
                }
               
            }
            return json;
        }

    }
}